# coding: utf-8
# Author: legend
# @Time： 2020/8/27 下午10:05
from utils.messages.handler import BaseHandler
from websocket.messages import LatestDeviceCpuLoadResponse, MultiDeviceCpuLoadResponse


class LatestDeviceCpuLoadRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        device_id = kwargs.get('device_id')
        result_value = self.get_monitor_data(0, device_id, 'cpu.load.avg', 1)
        return {
            'data': result_value,
            'result_code': LatestDeviceCpuLoadResponse.ResultCode.NORMAL
        }


class MultiDeviceCpuLoadRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        device_id = kwargs.get('device_id')
        size = kwargs.get('size')
        data = self.get_monitor_data(0, device_id, 'cpu.load.avg', size)
        return {
            'result_code': MultiDeviceCpuLoadResponse.ResultCode.NORMAL,
            'data': list(reversed(data))
        }
